package cn.itcast.health.admin.controller;

import cn.itcast.health.admin.logs.OperationLog;
import cn.itcast.health.commons.entity.QueryPageBean;
import cn.itcast.health.commons.entity.Result;
import cn.itcast.health.commons.resources.MessageConstant;
import cn.itcast.health.commons.resources.RedisConstant;
import cn.itcast.health.commons.utils.LogUtils;
import cn.itcast.health.commons.utils.QiniuUtils;
import cn.itcast.health.commons.utils.UploadUtils;
import cn.itcast.health.interfaces.service.CheckGroupService;
import cn.itcast.health.interfaces.service.SetmealService;
import cn.itcast.health.pojo.CheckGroup;
import cn.itcast.health.pojo.CheckSG;
import cn.itcast.health.pojo.Setmeal;
import cn.itcast.health.pojo.SetmealG;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    //用于查询检查组
    @Reference
    private CheckGroupService checkGroupService;

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;


    @GetMapping("/findCheckgroupAll.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.QUERY, operationContent = "供检套餐添加数据时所需要的检查组的数据")
    public Result findCheckgroupAll(){
        Result result = checkGroupService.findCheckgroupAll();
        return result;
    }

    @PostMapping("/uploadPicture.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.UPLOAD, operationContent = "上传图片到七牛云上")
    public Result uploadPicture(@RequestParam("imgFile") MultipartFile file){
        Result result = null;
        try {
            //获取发过来的图片名
            String originalFilename = file.getOriginalFilename();
            //重置图片名
            String fileName = UploadUtils.generateRandonFileName(originalFilename);
            //上传到七牛云上传
            QiniuUtils.upload2Qiniu(file.getBytes(),fileName);
            //存在redis缓存
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            jedis.close();
            //返回
            result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        }catch (Exception e){
            result = new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return result;
    }

    @PostMapping("/addSetmealAndSetmealCheckgroup.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.ADD, operationContent = "添加套餐及套餐与检查组中间表的数据")
    public Result addSetmealAndSetmealCheckgroup(@RequestBody CheckSG checkSG){
        Result result = setmealService.addSetmealAndSetmealCheckgroup(checkSG);
        return result;
    }

    @GetMapping("/findByPageSetmealData.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.ADD, operationContent = "分页查询套餐数据")
    public Result findByPageSetmealData(QueryPageBean queryPageBean){
        Result result = setmealService.findByPageSetmealData(queryPageBean);
        return result;
    }

    @GetMapping("/findByIdsetmealAnaIdsData.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.QUERY, operationContent = "供检套餐修改时所需要的检查组及套餐与检查组中间表的所选的数据")
    public Result findByIdCheckitemAnaIdsData(Setmeal setmeal){
        Result result = setmealService.findByIdCheckitemAnaIdsData(setmeal);
        return result;
    }


    @PutMapping("/updateSetmealAndSetmealCheckGroupData.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.MODIFY, operationContent = "修改套餐及套餐与检组中间表的数据")
    public Result updateSetmealAndSetmealCheckGroupData(@RequestBody SetmealG setmealG){
        Result result = setmealService.updateSetmealAndSetmealCheckGroupData(setmealG);
        return result;
    }

    @DeleteMapping("/deleteByIdSetmealData.do")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.DELETE, operationContent = "物理删除套餐及套餐与检组中间表的数据")
    public Result deleteByIdSetmealData(@RequestBody Setmeal setmeal){
        Result result = setmealService.deleteByIdSetmealData(setmeal.getId());
        return result;
    }

}
